//#############################################################################
//
//! \file  Application\Isr.c
//!
//! \brief  Interrupt Service Routine
//
//  Author:
//  Group:          C2000
//  Target Device:  TMS320F2802x
//
//  (C) Copyright 2014, Texas Instruments, Inc.
//#############################################################################
// $TI Release: f2802x Support Library v210 $
// $Release Date: Mon Sep 17 09:13:31 CDT 2012 $
//#############################################################################

// **************************************************************************
// the includes
#include "Application/isr.h"


// **************************************************************************
// the defines


// **************************************************************************
// the globals


// **************************************************************************
// the functions

//interrupt void isr(void)
//{
//
//    // Acknowledge this interrupt to get more from group 1
//    PIE_clearInt(myPie, PIE_GroupNumber_1);
//
//}


/*
 * 此函数指GPIO12对应的按键，按下后设定转速加10
 */
interrupt void KEY_XINT1_isr(void)
{
    //Delay(10);
//    if(Rotate_set == 0){
//        Rotate_set = 80;
//    }else if(Rotate_set < 110 && Rotate_set >= 80){
//        Rotate_set += 10;
//    }else{
//        Rotate_set = 129;
//    }
    LED_toggle(LED2);
    LED_toggle(LED3);
    PIE_clearInt(myPie, PIE_GroupNumber_1);
}
/*
 * 此函数指GPIO0对应的按键，按下后设定转速减10
 */
interrupt void KEY_XINT2_isr(void)
{
    //Delay(10);
    if(Rotate_set == 129){
        Rotate_set = 110;
    }else if(Rotate_set <= 80){
        Rotate_set = 0;
    }else{
        Rotate_set -= 10;
    }
    PIE_clearInt(myPie, PIE_GroupNumber_1);
}
/*
 * 此函数指GPIO1，脉冲计数
 */
interrupt void Pulse_isr(void)
{

    Pulse_number +=1;
    PIE_clearInt(myPie, PIE_GroupNumber_12);
}
/*
 * 此函数是定时器，间隔0.01s查看一次脉冲数量得出转速，读完脉冲数归0
 */
int rotate[10];
interrupt void myTimer_CpuTimer0_isr(void)
{
//    int temp = 0;
//    int i;
    Rotate_rate = 35*Pulse_number*10*60/426 ;  //每分钟的转速，减速比是35
    Pulse_number = 0;
//    for(i=0;i<9;i++){
//        rotate[i] = rotate[i+1];
//    }
//    rotate[9] = Rotate_rate;
//    //LED_toggle(LED4);
//    int sum = 0;
//    for(i=0;i<10;i++){
//        sum += rotate[i];
//    }
//    Rotate_rate = sum/10;

    PIE_clearInt(myPie, PIE_GroupNumber_1);
    TIMER_clearFlag(myTimer0);
}

interrupt void LED_EPWM2_isr(void)
{
    //myCmpA1 +=100 ;
    //if(myCmpA1 >= 60000) myCmpA1 = 0;

    PWM_setCmpB(myPwm2,myCmpA1);
    PIE_clearInt(myPie, PIE_GroupNumber_3);
    PWM_clearIntFlag(myPwm2);
}

//利用adc进行读取速度
interrupt void myAdc_ADCINT_isr(void)
{
    Temp = ADC_readResult(myAdc, ADC_ResultNumber_0);
    //TempC = ADC_getTemperatureC(myAdc, Temp);
    // Acknowledge this interrupt to get more from group 1
    if(Temp - TempC>2000){
        Pulse_number +=1;
    }
    TempC = Temp;
    PIE_clearInt(myPie, PIE_GroupNumber_1);
    //void ADC_clearIntFlag(ADC_Handle adcHandle, const ADC_IntNumber_e intNumber)
    ADC_clearIntFlag(myAdc, ADC_IntNumber_1);
}



//显示设定转速和实际转速
void wShowRotate(void)
{

    int hundred = Rotate_rate / 100;
    int ten = (Rotate_rate - hundred * 100) / 10;
    int single = Rotate_rate % 10;

    if (hundred)
        wShowOLED16X8(1, 50, hundred);
    else
        wShowOLED16X8(1, 50, 30);

    if (ten)
        wShowOLED16X8(1, 50 + 8, ten);
    else if(hundred > 0)
        wShowOLED16X8(1, 50 + 8, 0);
    else
        wShowOLED16X8(1, 50 + 8, 30);

    wShowOLED16X8(1, 50 + 16, single);

    hundred = Rotate_set / 100;
    ten = (Rotate_set - hundred * 100)  / 10;
    single = Rotate_set % 10;
    if (hundred)
        wShowOLED16X8(3, 50, hundred);
    else
        wShowOLED16X8(3, 50, 30);

    if (ten)
        wShowOLED16X8(3, 50 + 8, ten);
    else if(hundred > 0)
        wShowOLED16X8(3, 50 + 8, 0);
    else
        wShowOLED16X8(3, 50 + 8, 30);

    wShowOLED16X8(3, 50 + 16, single);

    int thousond1 = myCmpA1 / 10000;
    int thousond = (myCmpA1 - 10000*thousond1) / 1000;
    hundred = (myCmpA1 - 10000*thousond1 - 1000*thousond) / 100;
    ten = (myCmpA1 - 10000*thousond1 - 1000*thousond - 100*hundred) / 10;
    single = myCmpA1 % 10;
    if (thousond1)
        wShowOLED16X8(5, 50, thousond1);
    else
        wShowOLED16X8(5, 50, 30);
    if(thousond)
        wShowOLED16X8(5, 50+8, thousond);
    else if(thousond1>0)
        wShowOLED16X8(5, 50+8, 0);
    else
        wShowOLED16X8(5, 50+8, 30);
    if(hundred)
        wShowOLED16X8(5, 50+16, thousond);
    else if(thousond1>0 | thousond>0)
        wShowOLED16X8(5, 50+16, 0);
    else
        wShowOLED16X8(5, 50+16, 30);
    if(ten)
        wShowOLED16X8(5, 50+24, thousond);
    else if(thousond1>0 | thousond>0 | hundred>0)
        wShowOLED16X8(5, 50+24, 0);
    else
        wShowOLED16X8(5, 50+24, 30);
    wShowOLED16X8(5, 50 + 32, single);


    //wShowOLED16X8(1, 50 + 24, 14);
/*
    int i = 0;
    wOLED_SetRank(1);
    wOLED_SetPage(5);
    for (i = 1; i <= 128; i++)
        wOLED_WriteData(0x00);

    wOLED_SetRank(14);
    wOLED_SetPage(5);
    for (i = 0; i < Rotate_rate; i++)
        wOLED_WriteData(0xFF);            //2#1111_1111#

    if (Rotate_rate < 100)
        wOLED_delay(1E5);
    else
    {
        wOLED_delay(3E6);
        wOLED_FullShow(0x00);
    }

*/
}

interrupt void mySCI_TXINT_isr(void)
{   if(false == SciSentOk)
    {
        SciSendPoint +=1 ;
        if('\0' != SciSendBuf[SciSendPoint] && SciSendPoint <=50)
            SCI_putData(mySci, SciSendBuf[SciSendPoint]);
        else
        {
            SciSentOk = true;
        }
    }

    // Acknowledge this interrupt to get more from group 1
    PIE_clearInt(myPie, PIE_GroupNumber_9);

}

//! \brief      Interrupt Service Routine
//! \param[in]  None
//! \param[out] None
interrupt void mySCI_RXINT_isr(void)
{   uint16_t ch;
    if(false == SciReceivedOk)
    {
        ch = SCI_getData(mySci);
        SciReceiveBuf[SciReceivePoint] = ch;
        SciReceivePoint += 1;
        if( SciReceivePoint >=50)
        {
            SciReceivePoint = 0;
        }

        if('\0' == ch)
        {
            SciReceivedOk = true;
        }

    }
    // Acknowledge this interrupt to get more from group 1
    PIE_clearInt(myPie, PIE_GroupNumber_9);

}



// end of file
